layui.use(['table', 'admin', 'ax', 'element', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
	var element = layui.element
    var laydate = layui.laydate;
    
    //执行一个laydate实例
    laydate.render({
      elem: '#condition' //指定元素
		,type: 'month'
    });
	
	//tab切换
	element.on('tab(doTabBrief)',function(data){
		console.log(data.index)
	})

    /**
     * 邮政对账管理
     */
    var ToolExpressReconciliationYz = {
        tableId: "toolExpressReconciliationYzTable"
    };

    /**
     * 邮政账单
     */
    var ToolExpressReconciliationYz2 = {
        tableId: "toolExpressReconciliationYzTable2"
    };

    /**
     * 软风账单
     */
    var ToolExpressReconciliationRf3 = {
        tableId: "toolExpressReconciliationRfTable3"
    };

    /**
     * 初始化表格的列
     */
    ToolExpressReconciliationYz.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'reconciliationMonth', sort: true, title: '对账月份'},
            {field: 'remarks', sort: true, title: '备注'},
            {field: 'createTime', sort: true, title: '创建时间'},
            {field: 'createUserName', sort: true, title: '创建用户'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    ToolExpressReconciliationYz.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(ToolExpressReconciliationYz.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    /**
     * 弹出添加对话框
     */
    ToolExpressReconciliationYz.openAddDlg = function () {
        layer.open({
            type: 2,
            title: '新增对账月份',
            area: ['900px', '400px'],
            anim: 5,
            content: Feng.ctxPath + '/toolExpressReconciliationYz/add',
            end: function () {
                table.reload(ToolExpressReconciliationYz.tableId);
            }
        });
    };

    /**
     * 导出excel按钮
     */
    ToolExpressReconciliationYz.exportExcel = function () {
        var checkRows = table.checkStatus(ToolExpressReconciliationYz.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击编辑
     *
     * @param data 点击按钮时候的行数据
     */
    ToolExpressReconciliationYz.openEditDlg = function (data) {
        layer.open({
            type: 2,
            title: '编辑对账月份',
            area: ['900px', '400px'],
            anim: 5,
            content: Feng.ctxPath + '/toolExpressReconciliationYz/edit?id='+data.id,
            end: function () {
                table.reload(ToolExpressReconciliationYz.tableId);
            }
        });
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    ToolExpressReconciliationYz.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/toolExpressReconciliationYz/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(ToolExpressReconciliationYz.tableId);
                table.reload(ToolExpressReconciliationYz2.tableId);
                table.reload(ToolExpressReconciliationRf3.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + ToolExpressReconciliationYz.tableId,
        url: Feng.ctxPath + '/toolExpressReconciliationYz/list',
        page: true,
        limit: 30,
        height: 370,
        toolbar: '#toolbarfrom',
        cellMinWidth: 100,
        cols: ToolExpressReconciliationYz.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        ToolExpressReconciliationYz.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {
        ToolExpressReconciliationYz.openAddDlg();
    });

    // 导出excel
    $('#btnExp').click(function () {
        ToolExpressReconciliationYz.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + ToolExpressReconciliationYz.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            ToolExpressReconciliationYz.openEditDlg(data);
        } else if (layEvent === 'delete') {
            ToolExpressReconciliationYz.onDeleteItem(data);
        }
    });

    /**
     * 初始化表格的列
     */
    ToolExpressReconciliationYz2.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'noNumber', sort: true, title: '序号'},
            {field: 'product', sort: true, title: '产品'},
            {field: 'orderNumber', sort: true, title: '邮件号'},
            {field: 'province', sort: true, title: '省份'},
            {field: 'ycProvince', sort: true, title: '云仓省份', style:'background-color: #dbf1e1;'},
            {field: 'provinceTrue', sort: true, title: '1', style:'background-color: #dbf1e1;'},
            {field: 'provinceName', sort: true, title: '寄达省名称'},
            {field: 'receivingTime', sort: true, title: '收寄时间'},
            {field: 'weightK', sort: true, title: '重量(克)'},
            {field: 'chargingWeightKg', sort: true, title: '计费重量(千克)'},
            {field: 'roundingWeightKg', sort: true, title: '取整重量'},
            {field: 'ycWeightKg', sort: true, title: '云仓重量(千克)', style:'background-color: #dbf1e1;'},
            {field: 'weightDifference', sort: true, title: '重量差异', style:'background-color: #dbf1e1;'},
            {field: 'postage', sort: true, title: '总邮资'},
            {field: 'postageCheck', sort: true, title: '邮资核对', style:'background-color: #dbf1e1;'},
            {field: 'firstWeight', sort: true, title: '首重', style:'background-color: #dbf1e1;'},
            {field: 'weightContinuation', sort: true, title: '续重', style:'background-color: #dbf1e1;'},
            {field: 'discount', sort: true, title: '折扣', style:'background-color: #dbf1e1;'},
            {field: 'postageDifference', sort: true, title: '邮资差异', style:'background-color: #dbf1e1;'},
            {field: 'customerName', sort: true, title: '客户', style:'background-color: #dbf1e1;'},
            {field: 'income', sort: true, title: '收入', style:'background-color: #dbf1e1;'},
            {field: 'profit', sort: true, title: '利润', style:'background-color: #dbf1e1;'},
            {field: 'weightRange', sort: true, title: '重量区间', style:'background-color: #dbf1e1;'},
            {field: 'quantity', sort: true, title: '数量', style:'background-color: #dbf1e1;'},
        ]];
    };

    /**
     * 初始化表格的列
     */
    ToolExpressReconciliationRf3.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'outNumber', sort: true, title: '出库单号'},
            {field: 'shopNumber', sort: true, title: '店铺编号'},
            {field: 'shopName', sort: true, title: '店铺名称'},
            {field: 'platform', sort: true, title: '平台站点'},
            {field: 'outWNumber', sort: true, title: '出仓单号'},
            {field: 'inOrderNumber', sort: true, title: '内部订单号'},
            {field: 'outTime', sort: true, title: '出库日期'},
            {field: 'states', sort: true, title: '状态'},
            {field: 'expressName', sort: true, title: '快递公司'},
            {field: 'orderNumber', sort: true, title: '快递单号'},
            {field: 'province', sort: true, title: '省份'},
            {field: 'remarks', sort: true, title: '备注'},
            {field: 'provinces', sort: true, title: '省'},
            {field: 'city', sort: true, title: '市'},
            {field: 'area', sort: true, title: '区'},
            {field: 'street', sort: true, title: '街道'},
            {field: 'nums', sort: true, title: '发货件数'},
            {field: 'chargingWeightKg', sort: true, title: '计费重量'},
            {field: 'roundingWeightKg', sort: true, title: '取整重量'},
            {field: 'expressMoney', sort: true, title: '快递费'},
            {field: 'unilateralism', sort: true, title: '面单费'},
            {field: 'cWeight', sort: true, title: '续重'},
            {field: 'standard', sort: true, title: '二三级中转标准'},
            {field: 'minimum', sort: true, title: '二三级中转最低'},
            {field: 'transferMoney', sort: true, title: '中转费'},
            {field: 'additional', sort: true, title: '地区加收'},
            {field: 'additionalE', sort: true, title: '疫情加收'},
            {field: 'additionalSpecial', sort: true, title: '特殊城市加收'},
            {field: 'additionalBusySeason', sort: true, title: '旺季加收'},
            {field: 'goBeyond', sort: true, title: '超区'},
            {field: 'sumExpressMoney', sort: true, title: '总快递费'},
        ]];
    };
    
    // 单击行事件
    table.on('row(' + ToolExpressReconciliationYz.tableId + ')', function (obj) {
        var data = obj.data;
		$('div[lay-id="' + ToolExpressReconciliationYz.tableId + '"]').find(".layui-table-body tr ").attr({ "style": "background:#FFFFFF" });//其他tr恢复原样
		$('div[lay-id="' + ToolExpressReconciliationYz.tableId + '"]').find(obj.tr.selector).attr({ "style": "background:#e6e6e6;" });//改变当前tr颜色
    	finddata(data.id);
    });
    
    var find = 0

    function finddata(id){
		$("#tabmsg").show()
    	$("#findid").val(id)
    	find++

	    // 渲染表格
	    var tableResult2 = table.render({
	        elem: '#' + ToolExpressReconciliationYz2.tableId,
	        url: Feng.ctxPath + '/toolExpressReconciliationYz/list2?reconciliationId='+id,
	        page: true,
	        limit: 120,
	        height: 390,
	        toolbar: '#toolbarfrom',
			limits:[120,1500,6000,10000],
	        cellMinWidth: 100,
	        cols: ToolExpressReconciliationYz2.initColumn()
	    });

	    // 渲染表格
	    var tableResult3 = table.render({
	        elem: '#' + ToolExpressReconciliationRf3.tableId,
	        url: Feng.ctxPath + '/toolExpressReconciliationRf/list?reconciliationId='+id+"&v1=yz",
	        page: true,
	        limit: 120,
	        height: 390,
	        toolbar: '#toolbarfrom',
			limits:[120,1500,6000,10000],
	        cellMinWidth: 100,
	        cols: ToolExpressReconciliationRf3.initColumn()
	    });

	    /**
	     * 点击查询按钮
	     */
	    ToolExpressReconciliationYz2.search = function () {
	        var queryData = {};
	        queryData['condition'] = $("#condition2").val();
	        table.reload(ToolExpressReconciliationYz2.tableId, {
	            where: queryData, page: {curr: 1},
	             done:function () {
	            }
	        });
	    };

	    /**
	     * 点击查询按钮
	     */
	    ToolExpressReconciliationRf3.search = function () {
	        var queryData = {};
	        queryData['condition'] = $("#condition3").val();
	        table.reload(ToolExpressReconciliationRf3.tableId, {
	            where: queryData, page: {curr: 1},
	             done:function () {
	            }
	        });
	    };
	
	    /**
	     * 导出excel按钮
	     */
	    ToolExpressReconciliationYz2.exportExcel = function () {
	        var checkRows = table.checkStatus(ToolExpressReconciliationYz2.tableId);
	        if (checkRows.data.length === 0) {
	            Feng.error("请选择要导出的数据");
	        } else {
	            table.exportFile(tableResult2.config.id, checkRows.data, 'xls');
	        }
	    };

	    /**
	     * 导出excel按钮
	     */
	    ToolExpressReconciliationRf3.exportExcel = function () {
	        var checkRows = table.checkStatus(ToolExpressReconciliationRf3.tableId);
	        if (checkRows.data.length === 0) {
	            Feng.error("请选择要导出的数据");
	        } else {
	            table.exportFile(tableResult3.config.id, checkRows.data, 'xls');
	        }
	    };

	    if(find == 1){
			 // 导入excel
			 $('#btnExpimport2').click(function () {
		        layer.open({
		            type: 2,
		            title: 'excel导入',
		            area: ['1000px', '600px'],
		            anim: 5,
		            content: Feng.ctxPath + '/toolExpressReconciliationYz/toimportExcel2?reconciliationId='+$("#findid").val(),
		            end: function () {
		                table.reload(ToolExpressReconciliationYz2.tableId);
		                table.reload(ToolExpressReconciliationRf3.tableId);
		            }
		        });
			 });
		
		    $('#btnDelete2').click(function () {
		    	var checkStatus = table.checkStatus('toolExpressReconciliationYzTable2');
		        data = checkStatus.data;
		        if (data.length>0) {
		            var ids = [];
		            for (var i = 0; i < data.length; i++) {
		            	ids.push(data[i].id);
					}
		            var operation = function () {
		                var ajax = new $ax(Feng.ctxPath + "/toolExpressReconciliationYz/btnDelete2?ids="+ids, function (data) {
			                if(data.success){
			                	Feng.success("删除成功!");
			                	table.reload(ToolExpressReconciliationYz2.tableId);
			                } else {
			                    Feng.error("删除失败!" + data.message + "!");
			                }
		                }, function (data) {
		                    Feng.error("删除失败!" + data.responseJSON.message + "!");
		                });
		                ajax.start();
		            };
		            Feng.confirm("是否删除选中明细?", operation);
				} else {
					layer.alert('请至少选中一条数据');
				}
		    });
		
		    $('#btnDelete3').click(function () {
		    	var checkStatus = table.checkStatus('toolExpressReconciliationRfTable3');
		        data = checkStatus.data;
		        if (data.length>0) {
		            var ids = [];
		            for (var i = 0; i < data.length; i++) {
		            	ids.push(data[i].id);
					}
		            var operation = function () {
		                var ajax = new $ax(Feng.ctxPath + "/toolExpressReconciliationRf/btnDelete2?ids="+ids, function (data) {
			                if(data.success){
			                	Feng.success("删除成功!");
			                	table.reload(ToolExpressReconciliationRf3.tableId);
			                } else {
			                    Feng.error("删除失败!" + data.message + "!");
			                }
		                }, function (data) {
		                    Feng.error("删除失败!" + data.responseJSON.message + "!");
		                });
		                ajax.start();
		            };
		            Feng.confirm("是否删除选中明细?", operation);
				} else {
					layer.alert('请至少选中一条数据');
				}
		    });

		    $('#btnRevoke2').click(function () {
	            var operation = function () {
	                var ajax = new $ax(Feng.ctxPath + "/toolExpressReconciliationYz/btnRevoke2?reconciliationId="+$("#findid").val(), function (data) {
		                if(data.success){
		                	Feng.success("清空成功!");
			                table.reload(ToolExpressReconciliationYz2.tableId);
		                } else {
		                    Feng.error("清空失败!" + data.message + "!");
		                }
	                }, function (data) {
	                    Feng.error("清空失败!" + data.responseJSON.message + "!");
	                });
	                ajax.start();
	            };
	            Feng.confirm("确定清空明细吗?", operation);
		    });

		    $('#btnRevoke3').click(function () {
	            var operation = function () {
	                var ajax = new $ax(Feng.ctxPath + "/toolExpressReconciliationRf/btnRevoke2?reconciliationId="+$("#findid").val()+"&v1=yz", function (data) {
		                if(data.success){
		                	Feng.success("清空成功!");
			                table.reload(ToolExpressReconciliationRf3.tableId);
		                } else {
		                    Feng.error("清空失败!" + data.message + "!");
		                }
	                }, function (data) {
	                    Feng.error("清空失败!" + data.responseJSON.message + "!");
	                });
	                ajax.start();
	            };
	            Feng.confirm("确定清空明细吗?", operation);
		    });
		
		    // 搜索按钮点击事件
		    $('#btnSearch2').click(function () {
		        ToolExpressReconciliationYz2.search();
		    });
		
		    // 搜索按钮点击事件
		    $('#btnSearch3').click(function () {
		        ToolExpressReconciliationRf3.search();
		    });
		
		    // 导出excel
		    $('#btnExp2').click(function () {
		        ToolExpressReconciliationYz2.exportExcel();
		    });

		    $('#btnExp2all').click(function () {
        		window.location.href = Feng.ctxPath + '/toolExpressReconciliationYz/exportToExcel?reconciliationId='+$("#findid").val();
		    });
		
		    // 导出excel
		    $('#btnExp3').click(function () {
		        ToolExpressReconciliationRf3.exportExcel();
		    });

		    $('#btnExp3all').click(function () {
        		window.location.href = Feng.ctxPath + '/toolExpressReconciliationRf/exportToExcel?reconciliationId='+$("#findid").val()+"&v1=yz";
		    });
		}
	}


});
